using System;
using System.Windows.Input;

namespace Pyxis.Toolkit.Windows.Commands
{
	/// <summary>
	/// 
	/// </summary>
	public abstract class CommandBase : CommandCore, ICommand
	{
		#region ICommand Members

		public override Boolean CanExecute(Object parameter)
		{
			bool wCanExecute;
			try
			{
				wCanExecute = CanExecute();
			}
			catch (Exception)
			{
				wCanExecute = false;
			}
			return wCanExecute && !IsExecuting;
		}

		public override void Execute(Object parameter)
		{
			try
			{
				if (!((ICommand) this).CanExecute(parameter)) return;
				if (OnExecutingCore())
				{
					Execute();
				}
			}
			catch (Exception e)
			{
				SetFailure(e);
			}
		}

		#endregion

		protected abstract bool CanExecute();

		protected abstract void Execute();
	}
}